package edu.cs.usask.ca.silver.plugin.rcp.pages.preferences;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverPreferenceEntries;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;

/**
 * TODO: Add documentation
 * 
 * @author Yudi Xue
 * 
 */
public class UserLoginPreferencePage extends FieldEditorPreferencePage
		implements IWorkbenchPreferencePage {

	public UserLoginPreferencePage() {
		super(GRID);
	}

	private IWorkbench workbench;
	private boolean verification = true;

	StringFieldEditor passwdField, emailField;

	@Override
	public void init(IWorkbench workbench) {
		this.workbench = workbench;
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		String email = getPreferenceStore().getString(
				SilverPreferenceEntries.USER_EMAIL_LOGIN.name());
		String passwd = getPreferenceStore().getString(
				SilverPreferenceEntries.USER_PASSWORD.name());

	}

	@Override
	protected void createFieldEditors() {
		if (verification == false) {
			GridData data = new GridData();
			data.horizontalSpan = 2;
			CLabel label2 = new CLabel(this.getFieldEditorParent(),
					SWT.SHADOW_NONE);
			label2.setLayoutData(data);
			label2.setImage(Activator.getDefault().getWorkbench()
					.getSharedImages()
					.getImage(ISharedImages.IMG_OBJS_WARN_TSK));
			label2.setText("Unable to login current user");
		}

		emailField = new StringFieldEditor(
				SilverPreferenceEntries.USER_EMAIL_LOGIN.name(),
				SilverPreferenceEntries.USER_EMAIL_LOGIN
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_EMAIL_LOGIN
						.getLabelWidthToBeArranged(), getFieldEditorParent());

		passwdField = new StringFieldEditor(
				SilverPreferenceEntries.USER_PASSWORD.name(),
				SilverPreferenceEntries.USER_PASSWORD
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_PASSWORD
						.getLabelWidthToBeArranged(), getFieldEditorParent());

		passwdField
				.setValidateStrategy(StringFieldEditor.VALIDATE_ON_FOCUS_LOST);

		passwdField.getTextControl(getFieldEditorParent()).setEchoChar('*');

		addField(emailField);
		addField(passwdField);

		GridData data = new GridData(SWT.RIGHT, SWT.BOTTOM, true, true);
		data.horizontalSpan = 2;

	}

	@Override
	public boolean performOk() {
		//UPDATE PROJECTVIEWS
			return super.performOk();
	}
}
